//
//  SearchViewController.m
//  Beauty
//
//  Created by Snow on 16/9/12.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "SearchViewController.h"
#import "GXMacro.h"
#import "GXNetWorkingParser.h"
#import "NewProductCollectionViewCell.h"
#import "ModelOfHotSale.h"
#import "NewProductViewController.h"
@interface SearchViewController ()<UISearchBarDelegate, UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UISearchBar *searchBar;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property (nonatomic, strong) NSMutableArray *arrayOfSearch;
@property (nonatomic, copy) NSMutableString *highLightString;
@end

@implementation SearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(5, 5, SCREEN_WIDTH - 100, 30)];
    _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(5, 0, SCREEN_WIDTH - 100, 30)];
    [view addSubview:_searchBar];
    self.navigationItem.titleView = view;
    //searchbar基本属性
    self.searchBar.placeholder = @"请输入商品名称";
    //设置圆框
    self.searchBar.layer.masksToBounds = YES;
    self.searchBar.layer.cornerRadius = 10;
    self.searchBar.delegate = self;
}
/** 赋值 */
- (void)searchBar:(UISearchBar * _Nonnull)searchBar textDidChange:(NSString * _Nonnull)searchText
{
    _highLightString = searchText.mutableCopy;
    self.string = [NSString stringWithFormat:@"http://www.wantease.com/mobile1/index.php?act=goods&op=goods_list&gc_id=1535&keyword=%@&page=100", searchText];
    _arrayOfSearch = [NSMutableArray array];
    [GXNetWorkingParser GETWithURL:_string par:nil success:^(id responseObject) {
        NSDictionary *dic = responseObject[@"datas"];
        NSArray *arr = dic[@"goods_list"];
        for (NSDictionary *dict in arr) {
            ModelOfHotSale *model = [[ModelOfHotSale alloc] init];
            [model setValuesForKeysWithDictionary:dict];
            [_arrayOfSearch addObject:model];
        }
        [_collectionView reloadData];
    } filed:^(NSError *error) {
        
    }];
}
/** 搜索结果 */
- (void)searchBarSearchButtonClicked:(UISearchBar * _Nonnull)searchBar
{
    _layout = [[UICollectionViewFlowLayout alloc] init];
    _layout.itemSize = CGSizeMake((self.view.frame.size.width - 15) / 2, 280);
    _layout.minimumLineSpacing = 5;
    _layout.minimumInteritemSpacing = 5;
    _layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) collectionViewLayout:_layout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [self.view addSubview:_collectionView];
    [_collectionView registerClass:[NewProductCollectionViewCell class] forCellWithReuseIdentifier:@"NewProductCollectionViewCell"];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _arrayOfSearch.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NewProductCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"NewProductCollectionViewCell" forIndexPath:indexPath];
    ModelOfHotSale *model = [_arrayOfSearch objectAtIndex:indexPath.row];
    //关键字高亮
    NSRange range = [model.goods_name rangeOfString:_highLightString];//字符串范围
    NSMutableAttributedString *attributes = [[NSMutableAttributedString alloc]initWithString:model.goods_name];//可变字符串操作范围
    [attributes addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:18] range:range];//改变字体大小
    
    [attributes addAttribute:NSForegroundColorAttributeName value:[UIColor cyanColor] range:range];
    [cell.labelOfName setAttributedText:attributes];

    cell.labelOfPrice.text = [NSString stringWithFormat:@"¥ %@", model.goods_price ];
    [cell.imageOfGoods sd_setImageWithURL:[NSURL URLWithString:model.goods_image_url]];
    cell.backgroundColor = [UIColor redColor];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ModelOfHotSale *model = [_arrayOfSearch objectAtIndex:indexPath.row];
    NewProductViewController *new = [[NewProductViewController alloc] init];
    new.goods_id = model.goods_id;
    [self.navigationController pushViewController:new animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
